package best.driver.android.Activity.Order;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import best.driver.android.Activity.Base.BaseActivity;
import best.driver.android.Beans.OrderFeeDetail;
import best.driver.android.R;
import best.driver.android.RequestFactory;
import best.driver.android.Utils.Util;
import best.driver.android.View.PriceLabelView;
import best.driver.android.common.app.AppProxyFactory;
import best.driver.android.common.network.ApiObjectRequest;
import best.driver.android.common.network.ApiRequest;
import best.driver.android.common.network.BestError;
import best.driver.android.common.utils.StringUtils;
import butterknife.ButterKnife;
import butterknife.InjectView;
import de.greenrobot.event.EventBus;

/**
 * Created by xiangxuefeng on 15/9/15.
 */
public class OrderFeeActivity extends BaseActivity {

    @InjectView(R.id.tv_total_fee)
    TextView mTvTotalFee;

    @InjectView(R.id.plv_transit_fee_all)
    PriceLabelView mPlvTransitFeeAll;

    @InjectView(R.id.plv_transit_fee_first)
    PriceLabelView mPlvTransitFeeFirst;

    @InjectView(R.id.plv_transit_fee_second)
    PriceLabelView mPlvTransitFeeSecond;

    @InjectView(R.id.plv_transit_fee_third)
    PriceLabelView mPlvTransitFeeThird;

    @InjectView(R.id.plv_allowance)
    PriceLabelView mPlvAllowance;

    @InjectView(R.id.plv_wait_fee)
    PriceLabelView mPlvWaitFee;

    @InjectView(R.id.plv_familiar_fee)
    PriceLabelView mPlvFamiliarFee;

    @InjectView(R.id.plv_multi_fee)
    PriceLabelView mPlvMultiFee;

    @InjectView(R.id.plv_insurance_fee)
    PriceLabelView mPlvInsuranceFee;

    @InjectView(R.id.plv_subsidy_fee)
    PriceLabelView mPlvSubsidyFee;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_order_fee);
        ButterKnife.inject(this, view);
        view.setVisibility(View.GONE);
        String orderId = getIntent().getStringExtra("orderId");
        if(!TextUtils.isEmpty(orderId)) {
            getOrderFee(orderId);
        }
        setLeftIvBak(R.drawable.icon_navibar_back);
        setTopTopic("费用明细");
    }

    private void showFeeDetail(OrderFeeDetail detail) {
        if(detail != null) {
            mTvTotalFee.setText(Util.getOnePointNumber(detail.totalFee) + "");
            mPlvTransitFeeAll.setMoney(Util.getOnePointNumber(detail.transportFee1 + detail.transportFee2 + detail.transportFee3)
                    + "元", R.color.aaaa, 16);
            mPlvTransitFeeAll.setName("运费("+ Util.getOnePointNumber(detail.transportDistance / 1000.0)+"公里)", R.color.aaaa, 16);
            if(detail.transportFee1 > 1e-6) {
                mPlvTransitFeeFirst.setMoney(Util.getOnePointNumber(detail.transportFee1) + "元", R.color.a4, 14);
                mPlvTransitFeeFirst.setName("起步价(5公里内)", R.color.a4, 14);
                mPlvTransitFeeFirst.setVisibility(View.VISIBLE);
            }
            if(detail.transportFee2 > 1e-6) {
                mPlvTransitFeeSecond.setMoney(Util.getOnePointNumber(detail.transportFee2) + "元", R.color.a4, 14);
                mPlvTransitFeeSecond.setName("超5公里费", R.color.a4, 14);
                mPlvTransitFeeSecond.setVisibility(View.VISIBLE);
            }
            if(detail.transportFee3 > 1e-6) {
                mPlvTransitFeeThird.setMoney(Util.getOnePointNumber(detail.transportFee3) + "元", R.color.a4, 14);
                mPlvTransitFeeThird.setName("超20公里费", R.color.a4, 14);
                mPlvTransitFeeThird.setVisibility(View.VISIBLE);
            }
            if(detail.waitFee > 1e-6) {
                mPlvWaitFee.setMoney(Util.getOnePointNumber(detail.waitFee) + "元", R.color.aaaa, 16);
                mPlvWaitFee.setName("等待费(" + detail.waitTime + "分钟)", R.color.aaaa, 16);
                mPlvWaitFee.setVisibility(View.VISIBLE);
            }

            if(detail.familiarFee > 1e-6) {
                mPlvFamiliarFee.setMoney(Util.getOnePointNumber(detail.familiarFee) + "元", R.color.aaaa, 16);
                mPlvFamiliarFee.setName("小费" , R.color.aaaa, 16);
                mPlvFamiliarFee.setVisibility(View.VISIBLE);
            }

            if(detail.multipointFee > 1e-6) {
                mPlvMultiFee.setMoney(Util.getOnePointNumber(detail.multipointFee) + "元", R.color.aaaa, 16);
                mPlvMultiFee.setName("多点提卸货费", R.color.aaaa, 16);
                mPlvMultiFee.setVisibility(View.VISIBLE);
            }

            if(detail.longDistanceSubsidy > 1e-6) {
                mPlvSubsidyFee.setMoney(Util.getOnePointNumber(detail.longDistanceSubsidy) + "元", R.color.aaaa, 16);
                mPlvSubsidyFee.setName("补贴", R.color.aaaa, 16);
                mPlvSubsidyFee.setVisibility(View.VISIBLE);
            }

            if(detail.premium < -0.01) {
                mPlvInsuranceFee.setMoney(Util.getOnePointNumber(detail.premium) + "元", R.color.aaaa, 16);
                mPlvInsuranceFee.setName("保险费", R.color.aaaa, 16);
                mPlvInsuranceFee.setVisibility(View.VISIBLE);
            }
        }
    }

    private void getOrderFee(String orderId) {
        showWaiting();
        ApiObjectRequest<OrderFeeDetail> request = RequestFactory.createGetOrderFeeDetailRequest(orderId);
        request.setListener(new ApiRequest.ApiRequestListener<OrderFeeDetail>() {
            @Override
            public void onRequestSuccess(OrderFeeDetail response) {
                hideWaiting();
                view.setVisibility(View.VISIBLE);
                showFeeDetail(response);
            }

            @Override
            public void onRequestError(BestError error) {

            }
        });
        AppProxyFactory.getProxy().getNetworkManager().enqueueRequest(request);
    }
}
